import { _decorator, Button, Label } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { gm } from '../../../../scripts/managers/GameManager';
import { StageView } from '../GamePanel/2d/StageView';
import { lm } from '../../LobbyManager';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { DevConfig } from '../../../../scripts/managers/DevConfig';
import { thinking_sdk_m } from '../../../../scripts/managers/SDK/ThinkingCocosSDKManager';
import { ShopPanel } from '../ShopPanel/ShopPanel';
import { BuyHeartPanel } from '../BuyHeartPanel/BuyHeartPanel';
import { FlowStatus } from '../../Enums';
import { Observable, of } from 'rxjs';
import { am } from '../../../../scripts/managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('EditorGameLosePanel')
export class EditorGameLosePanel extends BasePanel {

    stageView: StageView = null;

    @property(Button)
    reviveButton: Button = null;

    @property(Label)
    reviveButtonLabel: Label = null;

    @property(Button)
    retryButton: Button = null;

    @property(Button)
    exitButton: Button = null;

    @property(Button)
    editButton: Button = null;

    needCoin = 0;

    willBind(): void {
        this.needCoin = DevConfig.COIN_LOSE_CONTINUE_NEED * (gm.continueCount.value + 1);
        this.reviveButtonLabel.string = `(${this.needCoin} Coin) Revive`;
    }

    useObserves() {
        return [
            gm.editEnabled.subscribe(_ => this.editButton.node.active = _),
            BindButton(this.reviveButton).subscribe(this.reviveButtonClicked.bind(this)),
            BindButton(this.retryButton).subscribe(this.retryButtonClicked.bind(this)),
            BindButton(this.exitButton).subscribe(this.exitButtonClicked.bind(this)),
            BindButton(this.editButton).subscribe(this.editButtonClicked.bind(this)),
        ];
    }

    willOpen(): Observable<string> {
        am.playSfx({ clipName: `ab_lobby/Audio/sfx/LevelFailed` });
        return of(null);
    }

    willClose(): Observable<string> {
        this.noninteractive();
        return of(null);
    }

    noninteractive() {
        this.reviveButton.interactable = false;
        this.exitButton.interactable = false;
        this.retryButton.interactable = false;
    }

    reviveButtonClicked() {
        if (!this.stageView.waitingPlacehasAnimating() && !this.stageView.queueHasAnimating()) {
            if (udm.coin.value >= this.needCoin) {
                thinking_sdk_m.requestTrack('Coins_Use', {
                    CoinNum: this.needCoin,
                    Type: 'Revive',
                    IsNew: udm.isNew.value,
                    Level_ID: udm.stageNum.value,
                });
                gm.reviveTimes.value++;
                udm.gameRevive(this.stageView.stageInfo, 'Gold');
                udm.coin.value -= this.needCoin;
                udm.save();
                gm.coinUse.value += this.needCoin;
                if (!this.stageView.waitingPlacehasAnimating() && !this.stageView.queueHasAnimating()) {
                    EditorGameLosePanel.CloseProcess(null, 'revive');
                } else {
                    lm.frontTipShow('Please wait...');
                }
            } else {
                thinking_sdk_m.requestTrack('Shoping_Open', {
                    OpenType: 'NoGold',
                    OpenWay: 'Result',
                    IsNew: udm.isNew.value,
                    Level_ID: udm.stageNum.value,
                });
                ShopPanel.StartProcess().subscribe();
            }
        } else {
            lm.frontTipShow('Please wait...');
        }
    }

    retryButtonClicked() {

        let heartCount = udm.getHeartCount();
        if (udm.getRemainMsToOverInfHeart() > 0 || heartCount >= 2) { // 如果有两颗心，那么扣掉一颗还剩一颗，可以直接重开游戏
            udm.gameLose(this.stageView.stageInfo, 'lose_restart');
            gm.loadGameCommand.execute();
            EditorGameLosePanel.CloseProcess(null, 'again');
        } else if (heartCount == 1) { // 如果只有一颗心，那么本局是失败的，扣过之后没有心了不能重新开始，所有要求先补充心
            BuyHeartPanel.StartProcess().subscribe(_ => {
                if (_ == 'continue') {
                    udm.gameLose(this.stageView.stageInfo, 'lose_restart');
                    gm.loadGameCommand.execute();
                    EditorGameLosePanel.CloseProcess(null, 'again');
                }
            });
        } else {
            thinking_sdk_m.requestTrack('Shoping_Open', {
                OpenType: 'NoEnergy',
                OpenWay: 'Result',
                IsNew: udm.isNew.value,
                Level_ID: udm.stageNum.value,
            });
            ShopPanel.StartProcess().subscribe();
        }

        // EditorGameLosePanel.CloseProcess(null, 'retry');
    }

    exitButtonClicked() {
        EditorGameLosePanel.CloseProcess(null, 'exit');
        udm.gameLose(this.stageView.stageInfo, 'lose_quit');
        lm.flowStatus.value = FlowStatus.Lobby;
        EditorGameLosePanel.CloseProcess(null, 'quit');
    }

    editButtonClicked() {
        EditorGameLosePanel.CloseProcess(null, 'edit');
    }


    // TODO:
    buyButtonClicked() {
    }
}


